﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;

public partial class SystemManage_DataBackBf : System.Web.UI.Page
{
    Db List = new Db();
    public static string sDataName, sDataUrl, sLjUr, sLjUser, sLjPas, sBfName, sMapFileName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }

        if (!Page.IsPostBack)
        {

            BindAttribute();
            string SQL_GetList1 = "select * from qp_hr_DataMap ";
            OleDbDataReader NewReader1 = List.GetList(SQL_GetList1);
            if (NewReader1.Read())
            {
                sMapFileName = NewReader1["MapFileName"].ToString();
                sDataUrl = "" + sMapFileName + "_" + System.DateTime.Now.Year.ToString() + "_" + System.DateTime.Now.Month.ToString() + "_" + System.DateTime.Now.Day.ToString() + "";
                LivePath.Text = "" + NewReader1["MapName"].ToString() + "" + sMapFileName + "_" + System.DateTime.Now.Year.ToString() + "_" + System.DateTime.Now.Month.ToString() + "_" + System.DateTime.Now.Day.ToString() + "";
            }
            NewReader1.Close();




            string SQL_GetList2 = "select * from qp_hr_DataLjUrl ";
            OleDbDataReader NewReader2 = List.GetList(SQL_GetList2);
            if (NewReader2.Read())
            {
                sLjUr = NewReader2["LjUr"].ToString();
                sLjUser = NewReader2["LjUser"].ToString();
                sLjPas = NewReader2["LjPas"].ToString();
                sBfName = NewReader2["BfName"].ToString();
            }
            NewReader2.Close();







        }

    }
    public void BindAttribute()
    {
        Button1.Attributes["onclick"] = "javascript:return showwait();";

    }



    protected void Button1_Click(object sender, EventArgs e)
    {


        try
        {
            SQLDMO.Backup backup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer sqlserver = new SQLDMO.SQLServerClass();
            sqlserver.LoginSecure = false;
            sqlserver.Connect("" + sLjUr + "", "" + sLjUser + "", "" + sLjPas + "");//连接服务器
            backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
            backup.Database = "" + sBfName + "";//要备份的数据库
            string str1 = this.LivePath.Text;
            backup.Files = str1;//备份路径
            backup.BackupSetName = "" + sDataUrl + "";//备份数据库名
            backup.BackupSetDescription = "数据库备份";
            backup.Initialize = true;
            backup.SQLBackup(sqlserver);
            List.InsertLog("备份数据库", "数据库备份");

            string sql_insertgly = "insert into qp_hr_DataBackList  (DataName,DataUrl,Username,Realname,Settimes) values ('" + sDataUrl + "','" + LivePath.Text + "','" + Session["username"] + "','" + Session["realname"] + "','" + System.DateTime.Now.ToString() + "')";
            List.ExeSql(sql_insertgly);

            this.Response.Write("<script language=javascript>alert('备份成功！');window.location.href='DataBackList.aspx'</script>");

        }
        catch(Exception err)
        {
            this.Response.Write("<script language=javascript>alert('备份失败请重试。或修改备份路径！');</script>");
        }





    }
}
